<?php
include("../includes/common.php");
if($islogin2==1){}else exit("<script language='javascript'>window.location.href='./login.php';</script>");
include './head.php';
?>
<?php
if($conf['settle_open']==0)exit('未开启申请提现');
$uid=$rs=$DB->query("select * from pay_user WHERE name={$pid}");
foreach($uid as $res){
 $usid=$res['id'];
}
$today=date("Y-m-d").' 00:00:00';
$notday=date("Y-m-d H:i:s");
$rs=$DB->query("SELECT * from pay_order where pid={$usid} and status=1 and endtime>='$today'");
$order_today=0;
while($row = $rs->fetch())
{
	$order_today+=$row['money'];
}
if($userrow['level']==1)
  $rat=$conf['money_rate'];
if($userrow['level']==2)
  $rat=$conf['money_rate_vip'];
if($userrow['level']==3)
  $rat=$conf['money_rate_svip'];
$enable_money=round($userrow['money']-$order_today*$rat/100,2);
$emoney=$enable_money*$rat/100;
$rmoney=$enable_money*(1-$rat/100);
if(isset($_GET['act']) && $_GET['act']=='do'){
	if($_POST['submit']=='申请提现'){
		if($userrow['apply']==1){
			exit("<script language='javascript'>alert('你今天已经申请过提现，请勿重复申请！');history.go(-1);</script>");
		}
		if($enable_money<$conf['settle_money']){
			exit("<script language='javascript'>alert('可提现余额不足！');history.go(-1);</script>");
		}
		if($userrow['type']==2){
			exit("<script language='javascript'>alert('您的商户出现异常，无法提现');history.go(-1);</script>");
		}
		$sqs=$DB->exec("update `pay_user` set `apply` ='1' where `name`='$pid'");//限制一日一次提现
        if($userrow['level']>=3 && $userrow['settle_id']==1){//钻石会员级别进行快速提现
          	$ins=$DB->exec("INSERT INTO `pay_settle`(`pid`, `batch`, `type`, `username`, `account`, `money`, `fee`, `status`, `transfer_status`,`time`) VALUES ({$pid},'T0_tixian',1,'{$userrow['username']}','{$userrow['account']}',{$emoney},{$rmoney},0,0,'{$notday}')");
          	if($ins){
              	$retid=$DB->query("select max(id) from `pay_settle` where pid={$usid}")->fetch();
              	//exit("<script language='javascript'>alert('{$retid[0]}');history.go(-1);</script>");
              	$datta=array(
                	'id'=>$retid[0],
                  	'pvkey'=>Alipay_PrivateKey
                );
              	//$_SESSION['privatekey']=Alipay_PrivateKey;
              	//echo "<script>window.location.href='./transfer_do_T0.php?id={$retid[0]}';</script>";
              	if(!$DB->exec("update `pay_user` set `money` =0 where `name`='$pid'"))
                  exit("<script language='javascript'>alert('提现失败！返回信息：用户信息设置失败！');history.go(-1);</script>");
              	
              	$uri='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
              	//print_r(dirname($uri)."/transfer_do_T0.php");
            	$mysub= spost(dirname($uri)."/transfer_do_T0.php",$datta);//提现信息提交
              	$ret=json_decode($mysub,true);
              	if($ret['code']==0 && $ret['msg']=='success'){
                  $DB->exec("update `pay_settle` set `status` =1 where `id`='{$retid[0]}'");
                  $DB->exec("update `pay_user` set `apply` ='0' where `name`='$pid'");//钻石会员无限次提现
                  exit("<script language='javascript'>alert('提现成功！返回信息：{$ret['result']}');history.go(-1);</script>");
                }
              	else{
                  $DB->exec("update `pay_user` set `money` ={$userrow['money']} where `name`='$pid'");
                  $DB->exec("update `pay_user` set `apply` ='0' where `name`='$pid'");//钻石会员无限次提现
                  exit("<script language='javascript'>alert('提现失败！错误信息：{$ret['msg']}{$ret['result']}');history.go(-1);</script>");
                }
            }else{
              	$DB->exec("update `pay_user` set `apply` ='0' where `name`='$pid'");//钻石会员无限次提现
            	exit("<script language='javascript'>alert('尊敬的钻石会员，本次T0快速提现失败！请联系管理员！');history.go(-1);</script>");
            }
			//exit("<script language='javascript'>alert('您不是尊贵的钻石vip会员，无法享受T+0快速提现，请找管理员升级！');history.go(-1);</script>");
		}
		exit("<script language='javascript'>alert('申请提现成功！T0结算仅限于支付宝结算方式！');history.go(-1);</script>");
	}
}


function spost($uri,$data){
	$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $uri);
curl_setopt ( $ch, CURLOPT_POST, 1);
curl_setopt ( $ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt ( $ch, CURLOPT_POSTFIELDS,$data);
$return = curl_exec ($ch);
curl_close ($ch);
return $return;
}

?>
<div class="clearfix"></div>  
  <div class="content-wrapper">
    <div class="container-fluid">
     <div class="row pt-2 pb-2">
        <div class="col-sm-9">
            <h4 class="page-title">申请提现</h4>
            <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="javaScript:void();">申请</a></li>
            <li class="breadcrumb-item"><a href="javaScript:void();">提现</a></li>
         </ol>
       </div>
     </div>
      <div class="row">
        <div class="col-lg-8">
           <div class="card">
            <div class="card-body">
                <div class="tab-pane" id="edit">           
                     <form action="./apply.php?act=do" method="post">
                        <div class="form-group row">
                            <label class="col-lg-3 col-form-label form-control-label" id="typename">支付宝账号:</label>
                            <div class="col-lg-9">
                                <input class="form-control" type="text" name="account" value="<?php echo $userrow['account']?>" placeholder="请输入支付宝账号" disabled>
                            </div>
                        </div>
                        <div class="form-group row">
                            <label class="col-lg-3 col-form-label form-control-label">支付宝姓名:</label>
                            <div class="col-lg-9">
                                <input class="form-control" type="text" name="username" value="<?php echo $userrow['username']?>" placeholder="请输入支付宝姓名" disabled>

                            </div>

                        </div>
                        <div class="form-group row">
                            <label class="col-lg-3 col-form-label form-control-label">当前余额:</label>
                            <div class="col-lg-9">
                                <input class="form-control" type="text" name="money" value="<?php echo $userrow['money']?>" disabled>
                            </div>
                        </div>
                        <div class="form-group row">
                            <label class="col-lg-3 col-form-label form-control-label">可提现余额:</label>
                            <div class="col-lg-9">
                                <input class="form-control" type="text" name="tmoney" value="<?php echo $enable_money?>" disabled>
                            </div>
                        </div>
                       
                        <div class="form-group row">
                            <label class="col-lg-3 col-form-label form-control-label"></label>
                            <div class="col-lg-9">
                                <input type="submit" name="submit" value="申请提现" class="btn btn-secondary btn-square waves-effect waves-light m-1">
                        
                         </div>
                        </div>
                        </form>
                </div>
            </div>
        </div>
      </div>
     <div class="col-lg-8">
          <div class="card">
            <div class="card-header text-uppercase">关于手动提现的注意事项：</div>
             <div class="card-body">
                <ul class="list-group">
              <li class="list-group-item list-group-item-primary">1.当商户余额达到本站<font color="red">“手动结算金额标准”</font>即可申请结算！</li>
              <li class="list-group-item list-group-item-primary">2.手动结算功能可以让您在商户余额未达到<font color="red">“每日自动结算金额标准”</font>时向管理员申请结算！<br/>3.款项将扣除<?php $a=100;$b=$conf['settle_rate'];echo $a*$b?>%的手续费，在T+1工作日内结算到您的指定账户中；结算时如手续费不足<font color=red"><?php echo $conf['settle_fee_min']; ?>元</font>按<font color="red"><?php echo $conf['settle_fee_min']; ?>元</font>收取，请知悉！<code>每日自动结算金额标准：</code>商户余额满<?php echo $conf['settle_money']; ?>元，系统每日自动结算！<br/><code>申请手动结算金额标准：</code>商户余额满<?php echo $conf['settle_money']; ?>元，才可申请手动结算！</li>
                  </ul>
             </div>
          </div>
        </div>
      </div> 
    </div>
    </div>
   </div>
    <a href="javaScript:void();" class="back-to-top"><i class="fa fa-angle-double-up"></i> </a>
<?php include 'foot.php';?>